//
//  HeartRateSevenTimesCell.swift
//  DaRings
//
//  Created by HFY on 2022/11/21.
//

import UIKit
import MYModule

class HeartRateSevenTimesCell: TableViewCell {
    
    var heartRateModels: [SingleHeartRateModel]? {
        
        didSet {
            
            guard let models = heartRateModels else {
                return
            }
  
            let barHightLightColor = UIColor.HeartRate.theme
            let barNormalColor = UIColor.HeartRate.theme
            
            let barValues: [SevenTimeBarChartView.BarValue] = models.map {
                
                SevenTimeBarChartView.BarValue(time: $0.end, data: Double($0.heartRate))
            }
            
            let config = self.setupConfig(isEmpty: false)
            let datas = barValues.map { $0.data }
            
            let xValueFormatter = DayAxisValueDateFormatter()
            xValueFormatter.axisValueArray = datas.map {
                
                if $0 == 0 {
                    return ""
                }
                
                return "\(Int($0))"
                
            }

            let barModel = SevenTimeBarChartView.BarsModel(color: barNormalColor, hightLightColor: barHightLightColor, bars: barValues, xAxisValueFormatter: xValueFormatter, xConfig: config)
            
            sevenTimeChartView.barModel = barModel
            
            
        }
    }


    override func makeUI() {
        
        contentView.backgroundColor =  UIColor.darkBackground
        sevenTimeChartView.cornerRadius = 18.0
        
        let config = self.setupConfig(isEmpty: true)
        
        sevenTimeChartView.barChartView.setConfig(config)
        sevenTimeChartView.barChartView.highlightFullBarEnabled = true
        sevenTimeChartView.barChartView.setBarCount(7)
        
        
        sevenTimeChartView.cornerRadius = 18.0
        contentView.addSubview(sevenTimeChartView)
        
        sevenTimeChartView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
    
    private func setupConfig(isEmpty: Bool) -> DFBarChartViewConfig {
        let config = DFBarChartViewConfig.init(
            xAxisLabelFont: Fonts.systemFont(ofSize: 10),
            xAxisLabelTextColor: isEmpty == false ? UIColor.assist_222222 : UIColor.assist_FFFFFF,
            xAxisLineColor: UIColor.HeartRate.theme,
            xAxisLineHeight: 1,
            highlightPerTapEnabled: true)
        return config
    }
    
    //MARK: - lazy load -
    
    private lazy var sevenTimeChartView: SevenTimeBarChartView = {
        let chartView = SevenTimeBarChartView()
        chartView.backgroundColor = UIColor.background
        chartView.setRoundedCorners(.all, radius: 18)
        return chartView
    }()
    

}
